package com.yugao.fintech.antelope.pay.payment.process.payorder;

import com.yugao.fintech.antelope.pay.common.assembly.PayOrderAssembly;
import com.yugao.fintech.antelope.pay.common.constants.Cs;
import com.yugao.fintech.antelope.pay.common.service.PayOrderService;
import com.yugao.fintech.antelope.pay.common.service.PayWayService;
import com.yugao.fintech.antelope.pay.common.utils.PayUtils;
import com.yugao.fintech.antelope.pay.model.entity.PayOrder;
import com.yugao.fintech.antelope.pay.model.entity.PayWay;
import com.yugao.fintech.antelope.pay.model.rqrs.pay.payorder.ContinuePayRQ;
import com.yugao.fintech.antelope.pay.payment.model.rqrs.payorder.UnifiedOrderExtRQ;
import com.yugao.fintech.antelope.pay.payment.model.rqrs.payorder.UnifiedOrderExtRS;
import com.yugao.fintech.antelope.pay.payment.model.rqrs.payorder.payway.AutoBarOrderExtRQ;
import com.yugao.fintech.framework.assistant.utils.StringUtils;
import com.yugao.fintech.framework.assistant.utils.exception.BizException;
import com.yugao.fintech.framework.core.response.Resp;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 统一支付订单处理
 */
@Service
public class UnifiedOrderProcess extends AbstractPayOrderProcess {
    @Autowired
    private PayWayService payWayService;

    @Autowired
    private PayOrderService payOrderService;

    @Autowired
    private PayOrderAssembly payOrderAssembly;

    /**
     * 统一下单接口
     */
    public Resp<?> unifiedOrder(UnifiedOrderExtRQ rq) {
        UnifiedOrderExtRQ bizRQ = buildBizRQ(rq);

        //实现子类的res
        Resp<?> apiRes = unifiedOrder(bizRQ.getWayCode(), bizRQ);
        if (apiRes.getData() == null) {
            return apiRes;
        }

        UnifiedOrderExtRS bizRes = (UnifiedOrderExtRS) apiRes.getData();

        // 聚合接口，返回的参数
        UnifiedOrderExtRS res = payOrderAssembly.toUnifiedOrderExtRS(bizRes);

        // 只有 订单生成（QR_CASHIER） || 支付中 || 支付成功返回该数据
        String orderStatus = bizRes.getOrderStatus();
        if (StringUtils.isNotEmpty(bizRes.getOrderStatus()) && (PayOrder.STATE_INIT.equals(orderStatus)
                || PayOrder.STATE_ING.equals(orderStatus) || PayOrder.STATE_SUCCESS.equals(orderStatus))) {
            res.setPayDataType(bizRes.buildPayDataType());
            res.setPayData(bizRes.buildPayData());
        }
        return Resp.ok(res);
    }


    /**
     * 继续去支付接口, 户很有可能点击付款后, 然后退出去了, 并没有付款, 此时订单处于未支付状态, 用户可以在 <br/>
     * 待支付页面, 继续支付未完成的订单
     */
    public Resp<?> continuePay(ContinuePayRQ rq) {
        if (StringUtils.isAllEmpty(rq.getPayOrderNo(), rq.getMchOrderNo())) {
            throw new BizException("payOrderNo 和 mchOrderNo 不能同时为空");
        }
        PayOrder payOrder = payOrderService.getMchOrder(rq.getMchNo(), rq.getPayOrderNo(), rq.getMchOrderNo());
        if (Objects.isNull(payOrder)) {
            throw new BizException("订单不存在");
        }

        UnifiedOrderExtRQ bizRQ = new UnifiedOrderExtRQ();
        // 实现子类的res
        Resp<?> apiRes = unifiedOrder(payOrder.getWayCode(), bizRQ, payOrder);
        if (apiRes.getData() == null) {
            return apiRes;
        }

        UnifiedOrderExtRS bizRes = (UnifiedOrderExtRS) apiRes.getData();

        // 聚合接口，返回的参数
        UnifiedOrderExtRS res = payOrderAssembly.toUnifiedOrderExtRS(bizRes);

        // 只有 订单生成（QR_CASHIER） || 支付中 || 支付成功返回该数据
        String orderStatus = bizRes.getOrderStatus();
        if (StringUtils.isNotEmpty(bizRes.getOrderStatus()) && (PayOrder.STATE_INIT.equals(orderStatus)
                || PayOrder.STATE_ING.equals(orderStatus) || PayOrder.STATE_SUCCESS.equals(orderStatus))) {
            res.setPayData(bizRes.buildPayData());
            res.setPayDataType(bizRes.buildPayDataType());
        }

//        return ApiRes.okWithSign(res, configContextQueryService.queryMchApp(rq.getMchNo(), rq.getAppId()).getAppSecret());
        return Resp.ok(res);
    }

    private UnifiedOrderExtRQ buildBizRQ(UnifiedOrderExtRQ rq) {
        // 支付方式  比如： ali_bar
        String wayCode = rq.getWayCode();

        // jsapi 收银台聚合支付场景 (不校验是否存在payWayCode)
        if (Cs.PAY_WAY_CODE.QR_CASHIER.equals(wayCode)) {
            return rq.buildBizRQ();
        }

        // 如果是自动分类条码
        if (Cs.PAY_WAY_CODE.AUTO_BAR.equals(wayCode)) {
            AutoBarOrderExtRQ bizRQ = (AutoBarOrderExtRQ) rq.buildBizRQ();
            wayCode = PayUtils.getPayWayCodeByBarCode(bizRQ.getAuthCode());
            rq.setWayCode(wayCode.trim());
        }

        if (payWayService.count(PayWay.lqw().eq(PayWay::getWayCode, wayCode)) <= 0) {
            throw new BizException("不支持的支付方式");
        }

        // 转换为 bizRQ
        return rq.buildBizRQ();
    }
}
